import java.awt.Point;
import java.util.List;
import java.util.Vector;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PGraphics;
import processing.core.PImage;
import TUIO.TuioCursor;
import TUIO.TuioObject;
import TUIO.TuioProcessing;
import TUIO.TuioTime;

public class assasinPuol extends PApplet {

	// velocidad
	float easing = 0.005f;
	float targetY;
	float targetX;

	float countKeyPressed = 10;

	TuioDemo tuioDemo = new TuioDemo();

	int countMovement = 0;

	Pulpo pulpo = new Pulpo();
	Pulpo pulpo2 = new Pulpo();

	public void setup() {
		size(1024, 768, OPENGL);
		targetX = random(width);
		targetY = random(height);
		// hint(ENABLE_OPENGL_4X_SMOOTH);
		stroke(0, 204);
		smooth();
		//frameRate(40);
		rectMode(CENTER);

		tuioDemo.setup(this);

		pulpo.setup(this);
		pulpo2.setup(this);
		
		pulpo2.attackToLeft(true);

	}

	public void draw() {

		tuioDemo.draw(this);

		int posX = tuioDemo.x;
		int posY = tuioDemo.y;

		if (posX > 100 && posX < width - 100) {
			targetX = posX;
			targetY = posY;
		}

		background(0);

		fill(111, 54, 185);

		pulpo.posx = mouseX;
		pulpo.posy = mouseY;
		pulpo.draw(this);

		pulpo2.posx = width - 200;
		pulpo2.posy = height / 2;
		pulpo2.draw(this);

		int movement = mouseX - pmouseX;
		if (PApplet.abs(movement) > Pulpo.MOVE_THRESHOLD) {
			println("start movement " + movement);
			countMovement++;
		} else {
			countMovement = 0;
		}

		if(pulpo.atack){
			isWounded(pulpo, pulpo2);
		}
		
		if(pulpo2.atack){
			isWounded(pulpo2, pulpo);
		}

	}

	private void isWounded(Pulpo pulpo, Pulpo pulpo2) {
		List<PulpoPoint> mask1 = pulpo2.mask;
		for (PulpoPoint point1 : mask1) {
			if (point1 != null) {
				if (pulpo.atackMask != null) {
					for (Point point : pulpo.atackMask) {
						if (point != null
								&& dist(point.x, point.y, point1.x, point1.y) < 8) {
							ellipse(point.x, point.y, 33, 33);
							pulpo2.wounded(point1.armNum, point1.falange);
						}
					}
				}
			}
		}
	}

	@Override
	public void keyPressed() {
		if (key == ' '){
			pulpo.atack();
		}else if (key == 'r'){
			pulpo.restart();
			pulpo2.restart();
		}else if(key == 'c'){
			pulpo2.atack();
		}
	}

	public void tail(int x, int units, float angle, int strokeColor) {

		float scaleValue = 3;

		pushMatrix();
		translate(x, 0);

		units *= 2 / (float) 3;

		for (int i = units; i > 0; i--) {

			strokeWeight(sqrt(i / 6));
			line(-i, 0, i, 0);
			fill(255);
			ellipse(-i, 0, 2 * scaleValue, 2 * scaleValue);
			ellipse(i, 0, 2 * scaleValue, 2 * scaleValue);
			stroke(strokeColor, 76);
			strokeWeight(i / 1.7f);
			fill(111, 54, 185);
			ellipse(0, 0, sqrt(i * scaleValue), sqrt(i * scaleValue));
			line(0, 0, 0, -8);
			translate(0, -10 * (scaleValue - 1));
			rotate(angle);
		}
		popMatrix();
	}

	public void addTuioObject(TuioObject tobj) {
		// applet.println("add object " + tobj.getSymbolID() + " (" +
		// tobj.getSessionID()
		// + ") " + tobj.getX() + " " + tobj.getY() + " "
		// + tobj.getAngle());
	}

	// called when an object is removed from the scene
	public void removeTuioObject(TuioObject tobj) {
		// applet.println("remove object " + tobj.getSymbolID() + " ("
		// + tobj.getSessionID() + ")");
	}

	// called when an object is moved
	public void updateTuioObject(TuioObject tobj) {
		// applet.println("update object " + tobj.getSymbolID() + " ("
		// + tobj.getSessionID() + ") " + tobj.getX() + " " + tobj.getY()
		// + " " + tobj.getAngle() + " " + tobj.getMotionSpeed() + " "
		// + tobj.getRotationSpeed() + " " + tobj.getMotionAccel() + " "
		// + tobj.getRotationAccel());
	}

	// called when a cursor is added to the scene
	public void addTuioCursor(TuioCursor tcur) {
		// println("add cursor " + tcur.getCursorID() + " (" +
		// tcur.getSessionID()
		// + ") " + tcur.getX() + " " + tcur.getY());
	}

	// called when a cursor is moved
	public void updateTuioCursor(TuioCursor tcur) {
		// println("update cursor " + tcur.getCursorID() + " ("
		// + tcur.getSessionID() + ") " + tcur.getX() + " " + tcur.getY()
		// + " " + tcur.getMotionSpeed() + " " + tcur.getMotionAccel());
	}

	// called when a cursor is removed from the scene
	public void removeTuioCursor(TuioCursor tcur) {
		// println("remove cursor " + tcur.getCursorID() + " ("
		// + tcur.getSessionID() + ")");
	}

	public class TuioDemo {

		/*
		 * TUIO processing demo - part of the reacTIVision project
		 * http://reactivision.sourceforge.net/
		 * 
		 * Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
		 * 
		 * This program is free software; you can redistribute it and/or modify
		 * it under the terms of the GNU General Public License as published by
		 * the Free Software Foundation; either version 2 of the License, or (at
		 * your option) any later version.
		 * 
		 * This program is distributed in the hope that it will be useful, but
		 * WITHOUT ANY WARRANTY; without even the implied warranty of
		 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
		 * General Public License for more details.
		 * 
		 * You should have received a copy of the GNU General Public License
		 * along with this program; if not, write to the Free Software
		 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
		 * USA
		 */

		// we need to import the TUIO library
		// and declare a TuioProcessing client variable

		TuioProcessing tuioClient;

		PApplet pApplet = null;

		// these are some helper variables which are used
		// to create scalable graphical feedback
		float cursor_size = 15;
		float object_size = 60;
		float table_size = 760;
		float scale_factor = 1;
		PFont font;
		public int x = 33;
		public int y = 33;

		public void setup(PApplet applet) {
			// size(screen.width,screen.height);
			// applet.size(1024, 768);
			this.pApplet = applet;
			// applet.noStroke();
			// applet.fill(0);

			// applet.loop();
			// applet.frameRate(30);
			// noLoop();

			// applet.hint(PApplet.ENABLE_NATIVE_FONTS);
			// font = applet.createFont("Arial", 18);
			scale_factor = applet.height / table_size;

			// we create an instance of the TuioProcessing client
			// since we add "this" class as an argument the TuioProcessing class
			// expects
			// an implementation of the TUIO callback methods (see below)
			tuioClient = new TuioProcessing(applet);
		}

		// within the draw method we retrieve a Vector (List) of TuioObject and
		// TuioCursor (polling)
		// from the TuioProcessing client and then loop over both lists to draw
		// the
		// graphical feedback.
		public void draw(PApplet applet) {
			// applet.background(255);
			// applet.textFont(font, 18 * scale_factor);
			// float obj_size = object_size * scale_factor;
			// float cur_size = cursor_size * scale_factor;
			//
			// Vector tuioObjectList = tuioClient.getTuioObjects();
			// for (int i = 0; i < tuioObjectList.size(); i++) {
			// TuioObject tobj = (TuioObject) tuioObjectList.elementAt(i);
			// applet.stroke(0);
			// applet.fill(0);
			// applet.pushMatrix();
			// applet.translate(tobj.getScreenX(applet.width),
			// tobj.getScreenY(applet.height));
			// applet.rotate(tobj.getAngle());
			// applet.rect(-obj_size / 2, -obj_size / 2, obj_size, obj_size);
			// applet.popMatrix();
			// applet.fill(255);
			// applet.text("" + tobj.getSymbolID(),
			// tobj.getScreenX(applet.width), tobj
			// .getScreenY(applet.height));
			// }

			Vector tuioCursorList = tuioClient.getTuioCursors();
			// println("numCursors "+tuioCursorList.size()+" x:"+x+" y:"+y);

			if (tuioCursorList.size() == 1) {

				TuioCursor tcur = (TuioCursor) tuioCursorList.elementAt(0);
				x = tcur.getScreenX(applet.width);
				y = tcur.getScreenY(applet.height);

				// TuioCursor tcur2 = (TuioCursor) tuioCursorList.elementAt(1);
				// int x2 = tcur.getScreenX(applet.width);
				// int y2 = tcur.getScreenY(applet.height);

				// x = (x1+x2)/2;
				// y = (y1+y2)/2;
			}
			// else if(tuioCursorList.size() == 2){
			// TuioCursor tcur = (TuioCursor) tuioCursorList.elementAt(0);
			// int x1 = tcur.getScreenX(applet.width);
			// int y1 = tcur.getScreenY(applet.height);
			//
			// TuioCursor tcur2 = (TuioCursor) tuioCursorList.elementAt(1);
			// int x2 = tcur.getScreenX(applet.width);
			// int y2 = tcur.getScreenY(applet.height);
			//
			// x = (x1+x2)/2;
			// y = (y1+y2)/2;
			// }else{
			// x = -1000;
			// y = -1000;
			// }

			// for (int i = 0; i < tuioCursorList.size(); i++) {
			// TuioCursor tcur = (TuioCursor) tuioCursorList.elementAt(i);
			// Vector pointList = tcur.getPath();
			//
			// if (pointList.size() > 0) {
			// applet.stroke(0, 0, 255);
			// TuioPoint start_point = (TuioPoint) pointList.firstElement();
			// ;
			// for (int j = 0; j < pointList.size(); j++) {
			// TuioPoint end_point = (TuioPoint) pointList.elementAt(j);
			// applet.line(start_point.getScreenX(applet.width), start_point
			// .getScreenY(applet.height), end_point.getScreenX(applet.width),
			// end_point.getScreenY(applet.height));
			// start_point = end_point;
			// }
			//
			// applet.stroke(192, 192, 192);
			// applet.fill(192, 192, 192);
			// applet.ellipse(tcur.getScreenX(applet.width),
			// tcur.getScreenY(applet.height),
			// cur_size, cur_size);
			// applet.fill(0);
			// applet.text("" + tcur.getCursorID(),
			// tcur.getScreenX(applet.width) - 5, tcur
			// .getScreenY(applet.height) + 5);
			// }
			// }

		}

		// these callback methods are called whenever a TUIO event occurs

		// called when an object is added to the scene

		// called after each message bundle
		// representing the end of an image frame
		public void refresh(TuioTime bundleTime) {
			pApplet.redraw();
		}

	}

	static public void main(String args[]) {
		PApplet.main(new String[] { "--present", "--bgcolor=#666666",
				"--stop-color=#cccccc", "assasinPuol" });
	}
}
